<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class Ajax extends AdminBase
{
    //标记违规词
    public function ajaxMarkViolation(){
        $content = $this->request->param('content');
        $content = markViolation($content);
        return json([
            'status' => 1,
            'msg' => '生成成功',
            'data' => $content
        ]);
    }

    //获取站点文章栏目
    public function getSiteArticleCategoryList($webid){
        if(empty($webid)){
            return json([
                'status' => 0,
                'msg' => '请求参数错误'
            ]);
        }
        $web = connect($webid);
        if(empty($web)){
            return json([
                'status' => 0,
                'msg' => '站点配置不完整'
            ]);
        }
        $data = null;
        switch ($web['cms_type']) {
            case 'eyoucms':
                $temp = Db::connect('dynamic')->name('arctype')->field('id,typename,parent_id')->where('channeltype',$web['cms_article'])->select()->toArray();
                foreach($temp as $v){
                    $data[] = [
                        'catid' => $v['id'],
                        'catname' => $v['typename'],
                        'parentid' =>$v['parent_id']
                    ];
                }
            break;
            case 'destoon':
                $data = Db::connect('dynamic')->name('category')->field('catid,catname,parentid')->where('moduleid',$web['cms_article'])->select()->toArray();
            break;
            case 'dedecms':
                
            break;
        }
        if(empty($data)){
            return json([
                'status' => 0,
                'msg' => '没有相关数据'
            ]);
        }
        $data = formatTree($data, 0, 1, $field = array('pid'=>'parentid','id'=>'catid','children'=>'children'));
        return json([
            'status' => 1,
            'msg' => '请求成功',
            'data' => $data
        ]);
    }

    //生成站点用户
    public function makeSiteUser(){
        $param = $this->request->param();
        if(empty($param['webid']) || empty($param['username'])){
            return json([
                'status' => 0,
                'msg' => '请求参数错误'
            ]);
        }
        $make = new \app\common\lib\MakeSiteUser();
        $res = $make->run($param['webid'],$param['username']);
        if(empty($res)){
            return json([
                'status' => 0,
                'msg' => '生成失败'
            ]);
        }
        return json([
            'status' => 1,
            'msg' => '生成成功'
        ]);
    }

}